package cn.yunhe.service.serviceImpl;

import cn.yunhe.dao.UserQuestionHisErrorpracDao;
import cn.yunhe.entity.Field;
import cn.yunhe.entity.Question;
import cn.yunhe.entity.QuestionType;
import cn.yunhe.entity.User;
import cn.yunhe.pojo.UserQuestionHisErrorprac;
import cn.yunhe.service.UserQuestionHisErrorpracService;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;

import java.util.List;
@Service("userQuestionHisErrorpracService")
public class UserQuestionHisErrorpracServiceImpl implements UserQuestionHisErrorpracService {
    @Autowired
    @Qualifier("userQuestionHisErrorpracDao")
    private UserQuestionHisErrorpracDao userQuestionHisErrorpracDao;
    @Override
    public List<UserQuestionHisErrorprac> getQuesHisErrorPrac(User user, int fieldId, int qyid) {
        Field field = new Field();
        field.setFieldId(fieldId);
        QuestionType qy = new QuestionType();
        qy.setId(qyid);
        List<UserQuestionHisErrorprac> userQuestionHisErrorpracs =  userQuestionHisErrorpracDao.getQuesHisErrorPrac(user,field,qy);
        return userQuestionHisErrorpracs;
    }

    @Override
    public int getAllErrorTitle(User user, int fieldId, int qyid) {
        Field field = new Field();
        field.setFieldId(fieldId);
        QuestionType qy = new QuestionType();
        qy.setId(qyid);
        int allErrorTotle = userQuestionHisErrorpracDao.getAllErrorTitle(user,field,qy);
        return allErrorTotle;
    }

    @Override
    public PageInfo pageSplit(User user, int fieldId, int qyid, int curPage, int perNum) {
        Field field = new Field();
        field.setFieldId(fieldId);
        QuestionType qy = new QuestionType();
        qy.setId(qyid);
        PageInfo pageInfo = new PageInfo();
        int getallTotle = userQuestionHisErrorpracDao.getAllErrorTitle(user,field,qy);
        pageInfo.setPages(getallTotle%perNum==0?getallTotle/perNum:getallTotle/perNum+1);
        PageHelper.startPage(curPage,perNum);
        List list = userQuestionHisErrorpracDao.getQuesHisErrorPrac(user,field,qy);
        pageInfo.setList(list);
        return pageInfo;
    }

    @Override
    public String getQyNameById(int id) {
       return userQuestionHisErrorpracDao.getQyNameById(id);
    }

    @Override
    public String getfieldNameById(int id) {
       return userQuestionHisErrorpracDao.getfieldNameById(id);
    }

    @Override
    public boolean insertpracanswer(int uid, int qyid, String data1) {
        int isrignt=1;
        String[] strings = data1.split(",");
        int qid = Integer.parseInt(strings[1]);
        int num = userQuestionHisErrorpracDao.selectQHisByid(qid);
        String answer =strings[0];
        Question question =  userQuestionHisErrorpracDao.selectQuesById(qid);
        System.out.println(question.getAnswer());
        if (question.getAnswer().equals(answer)){
            isrignt = 0;
        }
        if (num==0){
            return  userQuestionHisErrorpracDao.insertpracanswer(qid,uid,qyid,isrignt);
        }else {
            return  userQuestionHisErrorpracDao.updatepracuqh(qid,isrignt);
        }
    }


}





















